<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/headeradmin.php");
?>

<section>
<hgroup>
<h1>Kamer-onderdeel aanpassen</h1>
</hgroup>

<?php if(!isset($_POST['Submit'])) { 
    $query="SELECT * FROM roomitem WHERE Id='" .$_GET["id"]."'";
    $result = mysql_query($query) or die ("FOUT1: " . mysql_error());

    // gegevens ophalen en toekennen aan tijdelijke variabelen
    while ($rij = mysql_fetch_array($result)){
        $RoomId = $rij['RoomId'];
        $Name = $rij['Name'];
        $Activated = $rij['Activated'];
    }
?>
<form  name="kamerform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
	<tr>
            <td>Kamer:</td>
            <td>
                <select disabled="disabled" name="kamer">
                <option value="0">Kies een kamer</option>
                <?php
                $query3="SELECT * FROM roomtype";
                $result3 = mysql_query($query3) or die ("FOUT1: " . mysql_error());
                while ($rij2 = mysql_fetch_array($result3)){
                    if ($RoomId == $rij2['Id'])
                    {
                        echo "<option selected value=\"".$rij2['Id']."\">".$rij2['Name']."</option>";
                    }
                    else
                    {
                        echo "<option value=\"".$rij2['Id']."\">".$rij2['Name']."</option>";
                    }
                }
                ?>
            </td>
	</tr>
        <tr>
            <td>Onderdeel:</td>
            <td><input required="required" placeholder="Onderdeel" value="<?php echo $Name; ?>" name="onderdeel" type="text"></td>
	</tr>
        <tr>
            <td>Geactiveerd:</td>
            <td>
                <div class="onoffswitch">
                    <input type="checkbox" name="activatie" class="onoffswitch-checkbox" id="myonoffswitch" <?php if ($Activated == "T") { echo "checked"; } ?>>
                    <label class="onoffswitch-label" for="myonoffswitch">
                        <div class="onoffswitch-inner">
                            <div class="onoffswitch-active"><div class="onoffswitch-switch">JA</div></div>
                            <div class="onoffswitch-inactive"><div class="onoffswitch-switch">NEE</div></div>
                        </div>
                    </label>
                </div>
            </td>
        </tr>
 	<tr>
            <td><input type="hidden" name="id" value="<?php echo($_GET["id"]);?>" />
                <input type="hidden" name="kamer" value="<?php echo($_GET["kamer"]);?>" />
                <input type="submit" name="Submit" value="Opslaan">
            </td>
            <td><input type="button" value="Terug" onclick="javascript:history.back();" /></td>
        </tr>
</table>
</form>
<?php 
} 
else 
{	
	$error = array();
        
        if($_POST['onderdeel'] == "") {
		$error[] = 'Er is geen onderdeel ingevuld.';
	}
        
        if(isset($_POST['activatie'])) {
            $activatie = "T";
	}
        else
        {
            $activatie = "F";
        }
        
        $errors = sizeof($error); // aantal errors tellen
        if($errors != 0) { // Er is minstens 1 error
                echo "<span style=\"color:red\">Dit onderdeel kan niet worden aangepast omwille van de volgende reden(en):</span>";
                echo "<ul>";
                        for($i = 0; $i < $errors; $i++) {
                                echo "<span style=\"color:red\"><li>".$error[$i]."</li></span>";
                        }
                echo "</ul>";
        } 
        else 
        {
            $query = "UPDATE roomitem SET Name='$_POST[onderdeel]', Activated='$activatie' WHERE Id = '$_POST[id]'";
            mysql_query($query) or die ("FOUT2: " . mysql_error());
            echo "Het kamer-onderdeel is aangepast.<br>";
            echo "Klik <a href=\"kameronderdelen.php?kamer=".$_POST['kamer']."\">hier</a> om terug te gaan naar het overzicht.";
        }
} 
?>
</section>
<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>